package com.nbx.app.neheport.render;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.nbx.library.glutes.util;

import android.content.Context;
import android.opengl.GLU;

public class Render03 extends RenderBase {

    private float triangle[] = { 
            0.0f, 1.0f, 0.0f, 
            -1.0f, -1.0f, 0.0f, 
            1.0f, -1.0f, 0.0f, };
    
    private float colors[] = { 
            1.0f, 0.0f, 0.0f, 1.0f, 
            0.0f, 1.0f, 0.0f, 1.0f, 
            0.0f, 0.0f, 1.0f, 1.0f };

    private float rectangle[] = { 
            -1.0f, -1.0f, 0.0f, 
            1.0f, -1.0f, 0.0f, 
            -1.0f, 1.0f, 0.0f, 
            1.0f, 1.0f, 0.0f };

    private FloatBuffer triangleBuffer;
    private FloatBuffer colorBuffer;
    
    private FloatBuffer rectangleBuffer;
    
    public Render03(Context context) {
        super(context);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        triangleBuffer = util.createFloatBuffer(triangle);
        colorBuffer = util.createFloatBuffer(colors);
        rectangleBuffer = util.createFloatBuffer(rectangle);
        
        gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
        gl.glClearDepthf(1.0f); // Depth Buffer Setup
        gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
        gl.glDepthFunc(GL10.GL_LEQUAL);// The Type Of Depth Testing To Do
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  // Really Nice Perspective Calculations
    }
    
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
        gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
        gl.glLoadIdentity();                                   // Reset The Current Modelview Matrix
        
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glTranslatef(-1.5f, 0.0f, -10.0f); 
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuffer);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glColor4f(0.5f,0.5f,1.0f, 1.0f);
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, rectangleBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height); // Reset The Current Viewport

        gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
        gl.glLoadIdentity(); // Reset The Projection Matrix
        
        //gl.glOrthof(-2.0f, 2.0f, -2.0f, 2.0f, -1.0f, 1.0f);

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
        gl.glLoadIdentity(); // Reset The Modelview Matrix
    }
}
